package tp.pr2;

import java.util.StringTokenizer;

public class Parser
{
	public Parser()
	{

	}

	private static VerbCommands parseFirstWord(String firstWord)
	{
		VerbCommands verb = VerbCommands.UNKNOWN;
		VerbCommands[] validCommands = VerbCommands.values();
		for (int i = 0; i < validCommands.length; i++)
		{
			if (validCommands[i].toString().equalsIgnoreCase(firstWord))
				verb = validCommands[i];
		}

		return verb;
	}

	private static Directions parseDirection(String secondWord)
	{
		Directions dir = Directions.UNKNOWN;
		Directions[] validDirections = Directions.values();
		for (int i = 0; i < validDirections.length; i++)
		{
			if (validDirections[i].toString().equalsIgnoreCase(secondWord))
				dir = validDirections[i];
		}

		return dir;
	}
	

	public static Command nextCommand(String line)
	{
		Command command = null;
		String firstCommand = null;
		String secondCommand = null;

		StringTokenizer tkn = new StringTokenizer(line);
		int numberOfInstructions = tkn.countTokens();

		if ((numberOfInstructions > 2) || (numberOfInstructions == 0))
			command = new Command();
		else
		{
			firstCommand = tkn.nextToken();

			if (numberOfInstructions == 2)
				secondCommand = tkn.nextToken();

			VerbCommands firstVerbCommand=parseFirstWord(firstCommand);
			
			if (firstVerbCommand==VerbCommands.GO) {
				command = new Command(firstVerbCommand);
				command.setDirection(parseDirection(secondCommand));
			}
			
			else
				command=new Command(firstVerbCommand,secondCommand);
			
			
			

		}
		return command;
	}

	public static String getHelp()
	{
		return ("You are lost. You are alone. You wander around\n"
				+ "Your command words are:\n" +
				"    GO { NORTH|EAST|SOUTH|WEST }\n"+
"    HELP\n    QUIT\n    PICK <<id>>\n    USE <<id>>\n    LOOK [<<id>>]\n    EXAMINE");
	}

}
